/* -*- P4_16 -*- */
#include <core.p4>
#include <v1model.p4>

const bit<16> TYPE_IPV4 = 0x800;
const bit<16> TYPE_ARP  = 0x806;
const bit<16> TYPE_IPV6 = 0x86DD;
const bit<16> TYPE_SR   = 0x1234;
const bit<48> MCAST_IPV4_MAC = 0x01005e000001;
const bit<48> MCAST_IPV6_MAC = 0x3333ff000001;
const bit<48> MCAST_SR_MAC = 0x01005e000002;



/*************************************************************************
*********************** H E A D E R S  ***********************************
*************************************************************************/

typedef bit<48> macAddr_t;
typedef bit<32> ip4Addr_t;
typedef bit<128> ip6Addr_t;
typedef bit<9>  egressSpec_t;


header source_routing_t {
    bit<8> current_index;
    bit<8> path_len;
    bit<8> port0;
    bit<8> port1;
    bit<8> port2;
    bit<8> port3;
    bit<8> port4;
}

header ethernet_t {
    macAddr_t dstAddr;
    macAddr_t srcAddr;
    bit<16>   etherType;
}

header ipv4_t {
    bit<4>    version;
    bit<4>    ihl;
    bit<8>    diffserv;
    bit<16>   totalLen;
    bit<16>   identification;
    bit<3>    flags;
    bit<13>   fragOffset;
    bit<8>    ttl;
    bit<8>    protocol;
    bit<16>   hdrChecksum;
    ip4Addr_t srcAddr;
    ip4Addr_t dstAddr;
}

header arp_t {
    bit<16>   htype;
    bit<16>   ptype;
    bit<8>    hlen;
    bit<8>    plen;
    bit<16>   oper;
    macAddr_t sha;
    ip4Addr_t spa;
    macAddr_t tha;
    ip4Addr_t tpa;
}

header ipv6_t {
    bit<4> version;
    bit<8> trafficClass;
    bit<20> flowLabel;
    bit<16> payLoadLen;
    bit<8> nextHdr;
    bit<8> hopLimit;
    ip6Addr_t srcAddr;
    ip6Addr_t dstAddr;
}

struct metadata {
    /* empty */
}

struct headers {
    ethernet_t      ethernet;
    ipv4_t          ipv4;
    arp_t           arp;
    ipv6_t          ipv6;
    source_routing_t sr;
}
/*************************************************************************
*********************** P A R S E R  ***********************************
*************************************************************************/

parser MyParser(packet_in packet,
                out headers hdr,
                inout metadata meta,
                inout standard_metadata_t standard_metadata) {

    state start {
        transition parse_ethernet;
    }

    state parse_ethernet {
        packet.extract(hdr.ethernet);
        transition select(hdr.ethernet.etherType) {
            TYPE_IPV4: parse_ipv4;
            TYPE_ARP: parse_arp;
            TYPE_IPV6: parse_ipv6;
            TYPE_SR: parse_source_routing;
            default: accept;
        }
    }

    state parse_ipv4 {
        packet.extract(hdr.ipv4);
        transition accept;
    }

    state parse_arp {
        packet.extract(hdr.arp);
        transition accept;
    }

    state parse_ipv6 {
        packet.extract(hdr.ipv6);
        transition accept;
    }


    state parse_source_routing {
        packet.extract(hdr.sr);
    }
}

/*************************************************************************
************   C H E C K S U M    V E R I F I C A T I O N   *************
*************************************************************************/

control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
    apply {  }
}

/*************************************************************************
**************  I N G R E S S   P R O C E S S I N G   *******************
*************************************************************************/

control MyIngress(inout headers hdr,
                  inout metadata meta,
                  inout standard_metadata_t standard_metadata) {

    action drop() {
        mark_to_drop(standard_metadata);
    }

    action multicast_forward(egressSpec_t port) {
        standard_metadata.egress_spec = port;
    }

    action strip_sr_to_ipv4(macAddr_t dstMac, ip4Addr_t dstIp, egressSpec_t port) {
        hdr.sr.setInvalid();  // 移除 SR 头
        hdr.ethernet.etherType = TYPE_IPV4;
        hdr.ipv4.version = 4;
        hdr.ipv4.srcAddr = 0x0A000501;
        hdr.ipv4.dstAddr = dstIp;
        hdr.ipv4.ttl = 64;
        hdr.ethernet.dstAddr = dstMac;
        standard_metadata.egress_spec = port;
    }

    // 新增动作：移除 SR 头并封装为 IPv6
    action strip_sr_to_ipv6(macAddr_t dstMac, ip6Addr_t dstIp, egressSpec_t port) {
        hdr.sr.setInvalid();
        hdr.ethernet.etherType = TYPE_IPV6;
        hdr.ipv6.version = 6;
        hdr.ipv6.srcAddr = (bit<128>)0xfe80000000000000000000005678;
        hdr.ipv6.dstAddr = dstIp;
        hdr.ipv6.hopLimit = 64;
        hdr.ethernet.dstAddr = dstMac;
        standard_metadata.egress_spec = port;
    }

    action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) {
        standard_metadata.egress_spec = port;
        hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
        hdr.ethernet.dstAddr = dstAddr;
        hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
    }

    action ipv6_forward(macAddr_t dstAddr, egressSpec_t port) {
        standard_metadata.egress_spec = port;
        hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
        hdr.ethernet.dstAddr = dstAddr;
        hdr.ipv6.hopLimit = hdr.ipv6.hopLimit - 1;
    }

    action arp_reply(macAddr_t dstAddr, bit<9> port) {
        standard_metadata.egress_spec = port;
        hdr.ethernet.dstAddr = dstAddr;
    }


    action source_route_forward_0() {
        standard_metadata.egress_spec = (bit<9>)hdr.sr.port0;
        hdr.sr.current_index = hdr.sr.current_index + 1;
    }
    action source_route_forward_1() {
        standard_metadata.egress_spec = (bit<9>)hdr.sr.port1;
        hdr.sr.current_index = hdr.sr.current_index + 1;
    }
    action source_route_forward_2() {
        standard_metadata.egress_spec = (bit<9>)hdr.sr.port2;
        hdr.sr.current_index = hdr.sr.current_index + 1;
    }
    action source_route_forward_3() {
        standard_metadata.egress_spec = (bit<9>)hdr.sr.port3;
        hdr.sr.current_index = hdr.sr.current_index + 1;
    }

    action source_route_forward_4() {
        standard_metadata.egress_spec = (bit<9>)hdr.sr.port3;
        hdr.sr.current_index = hdr.sr.current_index + 1;
    }


    table ipv4_lpm {
        key = {
            hdr.ipv4.dstAddr: lpm;
        }
        actions = {
            ipv4_forward;
            drop;
            NoAction;
        }
        size = 1024;
        default_action = drop();
    }

    table ipv6_lpm {
        key = {
            hdr.ipv6.dstAddr: lpm;
        }
        actions = {
            ipv6_forward;
            drop;
            NoAction;
        }
        size = 1024;
        default_action = drop();
    }

    table arp_table {
        key = {
            hdr.arp.tpa: exact;
        }
        actions = {
            arp_reply;
            drop;
        }
        size = 1024;
        default_action = drop();
    }

    table sr_table {
        key = {
            hdr.sr.current_index : exact;
        }
        actions = {
            source_route_forward_0;
            source_route_forward_1;
            source_route_forward_2;
            source_route_forward_3;
            source_route_forward_4;
            drop;
            NoAction;
        }
        size = 4;
        default_action = drop();
    }

    table sr_terminate_table {
        key = {
            hdr.sr.current_index: exact;
        }
        actions = {
            strip_sr_to_ipv4;
            strip_sr_to_ipv6;
            drop;
        }
        size = 4;
        default_action = drop();
    }


      table multicast_table {
          key = {
              hdr.ethernet.dstAddr: exact;
          }
          actions = {
              multicast_forward;
              drop;
              NoAction;
          }
          size = 1024;
          default_action = drop();
      }

    apply {

        if (standard_metadata.ingress_port == 0 && hdr.sr.isValid()) {
            hdr.sr.current_index = 0;
        }

        if (hdr.sr.isValid()) {

            if (hdr.sr.path_len > 4 || hdr.sr.current_index >= hdr.sr.path_len) {
                mark_to_drop(standard_metadata);
                hdr.sr.setInvalid();
            } else {
                sr_table.apply();
                if (hdr.sr.current_index + 1 >= hdr.sr.path_len) {
                    sr_terminate_table.apply();
                }
                if (hdr.sr.current_index >= hdr.sr.path_len) {
                    hdr.sr.setInvalid();
                }
            }
        }
        else if (hdr.ipv4.isValid()) {
            ipv4_lpm.apply();
        }
        else if (hdr.arp.isValid()) {
            arp_table.apply();
        }
        else if (hdr.ipv6.isValid()) {
            ipv6_lpm.apply();
        }
    }
}

/*************************************************************************
****************  E G R E S S   P R O C E S S I N G   *******************
*************************************************************************/

control MyEgress(inout headers hdr,
                 inout metadata meta,
                 inout standard_metadata_t standard_metadata) {
    apply {  }
}

/*************************************************************************
*************   C H E K S U M    C O M P U T A T I O N   **************
*************************************************************************/

control MyComputeChecksum(inout headers hdr, inout metadata meta) {
     apply {
        update_checksum(
            hdr.ipv4.isValid(),
            { hdr.ipv4.version,
              hdr.ipv4.ihl,
              hdr.ipv4.diffserv,
              hdr.ipv4.totalLen,
              hdr.ipv4.identification,
              hdr.ipv4.flags,
              hdr.ipv4.fragOffset,
              hdr.ipv4.ttl,
              hdr.ipv4.protocol,
              hdr.ipv4.srcAddr,
              hdr.ipv4.dstAddr },
            hdr.ipv4.hdrChecksum,
            HashAlgorithm.csum16);
    }
}

/*************************************************************************
***********************  D E P A R S E R  *******************************
*************************************************************************/

control MyDeparser(packet_out packet, in headers hdr) {
    apply {
        packet.emit(hdr.ethernet);
        packet.emit(hdr.ipv4);
        packet.emit(hdr.arp);
        packet.emit(hdr.ipv6);
        packet.emit(hdr.sr);
    }
}

/*************************************************************************
***********************  S W I T C H  *******************************
*************************************************************************/

V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;
